<template>
  <section class="todoapp">
    <!-- 头部部分 -->
    <ToHeader @addList="addList"></ToHeader>
    <ToBody
      @delList="delList"
      :list="list"
      @changeStatus="changeStatus"
      @changeChecked="changeChecked"
      :isCheckAll="isCheckAll"
    ></ToBody>
    <ToFooter
      @clearDoneList="clearDoneList"
      :left="left"
      :isDoneAll="isDoneAll"
      v-model="type"
      v-show="list.length"
    ></ToFooter>

    <!-- 主体部分 -->

    <!-- 底部部分 -->
  </section>
</template>

<script>
import ToHeader from '@/components/ToHeader.vue'
import ToBody from '@/components/ToBody.vue'
import ToFooter from '@/components/ToFooter.vue'

export default {
  components: {
    ToBody,
    ToHeader,
    ToFooter
  },
  data () {
    return {
      list: [
        { id: 1, name: '吃饭', isDone: true },
        { id: 2, name: '睡觉', isDone: false },
        { id: 3, name: '打豆豆', isDone: true }
      ]
    }
  },
  methods: {
    delList (id) {
      this.list = this.list.filter(item => item.id !== id)
    },
    changeStatus (id) {
      const res = this.list.find(item => item.id === id)
      res.isDone = !res.isDone
    },
    addList (value) {
      const obj = { id: +new Date(), name: value, isDone: false }

      this.list = [obj, ...this.list]
    },
    changeChecked (flag) {
      this.list = this.list.filter(item => (item.isDone = flag))
    },
    clearDoneList () {
      this.list = this.list.filter(item => !item.isDone)
    },
    chanType (type) {
      this.type = type
    }
  },
  computed: {
    left () {
      return this.list.filter(item => !item.isDone).length
    },
    isDoneAll () {
      return this.list.some(item => item.isDone)
    },
    renderList () {
      switch (this.type) {
        case 'all':
          return this.list
        case 'active':
          return this.list.filter(item => !item.isDone)
        case 'completed':
          return this.list.filter(item => item.isDone)
        default:
          return this.list
      }
    },
    isCheckAll () {
      return this.list.every(item => item.isDone)
    }
  }
}
</script>

<style></style>
